package com.community.web.master.controller;


import com.community.common.result.Result;
import com.community.model.entity.GoodsCategory;
import com.community.web.master.domain.dto.GoodsCategoryDto;
import com.community.web.master.domain.dto.PageGoodsCategoryDto;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.service.IGoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/master/goodsCategory")
@Slf4j
@Api(tags = "商品类别接口")
public class GoodsCategoryController {
    @Autowired
    IGoodsCategoryService iGoodsCategoryService;
    @GetMapping
    @ApiOperation("查询所有类别")
    public Result<List<GoodsCategory>> List(){
        log.info("获取所有类别");
        return Result.success(iGoodsCategoryService.list().stream()
                .map(res -> {
                    if (res.getParentId() == null) {
                        res.setParentId(0);
                    }
                    return res;
                })
                .collect(Collectors.toList()));
    }

    @GetMapping("/pageQuery")
    @ApiOperation("分页查询")
    public Result<PageResult> pageQuery(PageGoodsCategoryDto pageGoodsCategoryDto){
        log.info("分页查询"+pageGoodsCategoryDto);
        PageResult result  = iGoodsCategoryService.pageQuery(pageGoodsCategoryDto);
        return Result.success(result);
    }

    @PostMapping("/add")
    @ApiOperation("新增分类")
    public Result add(@RequestParam Integer parentId,@RequestParam String categoryName){
        log.info("新增分类"+parentId+" "+categoryName);
        iGoodsCategoryService.add(parentId,categoryName);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改分类")
    public Result update(@RequestBody GoodsCategoryDto goodsCategoryDto){
        log.info("修改分类:"+goodsCategoryDto);
        iGoodsCategoryService.updateCategory(goodsCategoryDto);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据类别Id查询类别")
    public Result<GoodsCategory> getById(@PathVariable Integer id){
        log.info("根据类别id查询类别"+id);
        return Result.success(iGoodsCategoryService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据类别Id删除类别")
    public Result deleteById(@PathVariable Integer id){
        log.info("根据类别id删除类别"+id);
        iGoodsCategoryService.deleteById(id);
        return Result.success();
    }
}
